//
//  MTLoginViewController.m
//  ShiShiBang
//
//  Created by 宋宝强 on 16/9/9.
//  Copyright © 2016年 motianjituan. All rights reserved.
//

#import "MTLoginViewController.h"
#import "MTMiddleView.h"
#import "MTBottomView.h"
#import "MTRegisterViewController.h"
#import "UMSocial.h"
#import "MTForgetPwdViewController.h"
#import "MTHttpTools.h"
#import "MTTabBarController.h"


@interface MTLoginViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIView *middle;
@property (weak, nonatomic) IBOutlet UIView *bottom;

@end

@implementation MTLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    MTMiddleView *middleV = [MTMiddleView loadxib];
    MTBottomView *bottomV = [MTBottomView loadxib];
    [self.bottom addSubview:bottomV];
    [self.middle addSubview:middleV];

    [middleV.registerAccountBtn addTarget:self action:@selector(registerAccountBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [middleV.forgetPwdBtn addTarget:self action:@selector(forgetPwdBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [middleV.loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    middleV.pwdField.delegate = self;
    middleV.accountField.delegate = self;
    
    //share
    [bottomV.weiBoShareBtn addTarget:self action:@selector(weiBoShareBtn) forControlEvents:UIControlEventTouchUpInside];
    [bottomV.weChatShareBtn addTarget:self action:@selector(weChatShareBtn) forControlEvents:UIControlEventTouchUpInside];
    [bottomV.QQShareBtn addTarget:self action:@selector(QQShareBtn) forControlEvents:UIControlEventTouchUpInside];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    
}
#pragma mark ------监听点击
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
     MTMiddleView *middleV = self.middle.subviews[0];
    
    if (textField.tag == 1) {
        middleV.accountImage.image = [UIImage imageNamed:@"loginmy"];
        middleV.pwdImage.image = [UIImage imageNamed:@"lock·"];
    } else if (textField.tag != 1){
        middleV.accountImage.image = [UIImage imageNamed:@"my-未输入"];
        middleV.pwdImage.image = [UIImage imageNamed:@"locklook"];
    }
}

#pragma mark ------界面消失
- (IBAction)dismissClick:(id)sender {
    [self  dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark ------点击注册按钮
-(void)registerAccountBtnClick {
    MTRegisterViewController *vc = [[MTRegisterViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)forgetPwdBtnClick {
    MTForgetPwdViewController *vc = [[MTForgetPwdViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}
#pragma mark ------登录按钮点击
-(void)loginBtnClick {
    
    
// 检测登录账户和密码不为空
    
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
     MTMiddleView *loginView = _middle.subviews[0];
    
    dict[@"userName"] = [NSString stringWithFormat:@"%@",loginView.accountField.text];
    dict[@"password"] = [NSString stringWithFormat:@"%@",loginView.pwdField.text];
 
//@"http://120.76.242.81:8080/ssb-app/user/login.do"
    [MTHttpTools post:@"http://120.76.242.81:8080/ssb-app/user/login.do" params:dict success:^(id responseObj) {
        NSLog(@"%@",MTBaseURL(@"login.do"));

        if (responseObj[@"status"]) {
         
            [MTTool toolSaveToken:responseObj[@"token"]];
            
            MTTabBarController *vc = [[MTTabBarController alloc] init];
            [self dismissViewControllerAnimated:vc completion:^{
                [MBProgressHUD showSuccess:@"登录成功"];
            }];
        }
    } failure:^(NSError *error) {
        
    }];
}

// 约束生效
- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    // 设置登录view
    UIView *loginView = _middle.subviews[0];
    loginView.frame = _middle.bounds;
    
    // 设置快速登录view
    UIView *fastView = _bottom.subviews[0];
    fastView.frame = _bottom.bounds;
}

//监听键盘高度
- (void)keyboardWasShown:(NSNotification*)aNotification
{
    //键盘高度
    [UIView animateWithDuration:0.1 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, -100);
    }];
}

-(void)keyboardWillBeHidden:(NSNotification*)aNotification
{
    [UIView animateWithDuration:0.1 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, 0);
    }];    
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.1 animations:^{
       [self.view endEditing:YES];
    }];
    
}

#pragma mark ------分享
-(void)weiBoShareBtn {
     [self setupToThird:UMShareToSina];
}

-(void)weChatShareBtn {
    [self setupToThird:UMShareToWechatSession];
}

-(void)QQShareBtn {
    [self setupToThird:UMShareToQQ];
}

-(void)setupToThird:(NSString *)thirdPlatform {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:thirdPlatform];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
//            NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
            NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.thirdPlatformUserProfile, response.thirdPlatformResponse, response.message);
            
        }});
}
@end
